package com.util1;

//import com.auth0.jwt.JWT;
//import com.auth0.jwt.JWTVerifier;
//import com.auth0.jwt.algorithms.Algorithm;
//import com.auth0.jwt.interfaces.DecodedJWT;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;


/**
 * @Author 123
 * @Date 2022/2/9 21:06
 * @Version 1.0
 */

public class Demo {
//    public static void main(String[] args) {
//
//
//        Calendar c = Calendar.getInstance();
//        c.add(Calendar.MINUTE,10);
//
//        //颁发令牌
//        String token = JWT.create()//.withHeader()
//                .withClaim("id", 12)  //指定payload
//                .withClaim("name", "jack")//指定payload
//                .withExpiresAt(c.getTime())//指定令牌的过期时间
//                .sign(Algorithm.HMAC256("salt"));//签名
//
//        System.out.println(token);
//
//
//
//
//
//    }

//    public static void main(String[] args) {
//        //解析jwt令牌
//        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("salt")).build();
//        DecodedJWT verify = jwtVerifier.verify("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiamFjayIsImlkIjoxMiwiZXhwIjoxNjQ0MjIzNjkzfQ.UZY8yLkF6xEtlwDH1yzN3-_XYF4EK9GpYiCexKyko-0");
//        Integer id = verify.getClaim("id").asInt();
//        String name = verify.getClaim("name").asString();
//
//        System.out.println("id"+id+"name:"+name);
//
//
//    }


//    public static void main(String[] args) throws Exception{
//        //生成秘钥对
//        String privateFilePath = "E:\\key\\rsa.pri";
//        String publicFilePath = "E:\\key\\rsa.pub";
//        RsaUtils.generateKey(publicFilePath,privateFilePath,"fengmipuzi",2048);
//    }

    public static void main(String[] args) throws  Exception{
        //颁发令牌（私钥加密）
        //payload
        Map data = new HashMap(){{
            put("id",12);
            put("name","jack");
        }};

        //获取私钥
        PrivateKey privateKey = RsaUtils.getPrivateKey("E:\\key\\rsa.pri");


        String token = JwtUtils.generateTokenExpireInSeconds(data, privateKey, 10);

        System.out.println(token);


        Thread.sleep(15000);


        //解析令牌

        //获取公钥
        PublicKey publicKey = RsaUtils.getPublicKey("E:\\key\\rsa.pub");
        Map infoFromToken = (Map) JwtUtils.getInfoFromToken("asdfasdfasdfasdfasdfasd", publicKey, Map.class);

        System.out.println(infoFromToken.get("id"));
        System.out.println(infoFromToken.get("name"));
    }


}
